package com.beidasoft.web.util.encrypt.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.beidasoft.web.util.encrypt.EncAndDecProperties;
import com.beidasoft.web.util.encrypt.service.BDPEncryptStrategyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @Author zhangwj1
 * @Description TODO
 * @date 2022/10/9 10:27
 * @Version 1.0
 */
@Service("RSA")
public class RSAServiceImpl implements BDPEncryptStrategyService {
    @Resource
    private EncAndDecProperties encAndDecProperties;

    public RSAServiceImpl() {
    }

    public EncAndDecProperties.EncryptSecretKey.RSASecretKey getRSASecretKey() {
        return this.encAndDecProperties.getEncryptSecretKey().getRsa();
    }

    public Object getKey() {
        return SecureUtil.generateKeyPair("RSA");
    }

    public String encrypt(String content) {
        return this.encrypt(content, this.getRSASecretKey().getPublicKey());
    }

    public String encrypt(String content, String encryptKey) {
        String encode = Base64.encode(content);
        RSA rsa = new RSA(null, org.apache.commons.net.util.Base64.decodeBase64(encryptKey));
        byte[] encrypt = rsa.encrypt(encode, KeyType.PublicKey);
        return org.apache.commons.net.util.Base64.encodeBase64String(encrypt).replaceAll("\r\n","");
    }

    public String decrypt(String encryptStr) {
        return this.decrypt(encryptStr, this.getRSASecretKey().getPrivateKey());
    }

    public String decrypt(String encryptStr, String decryptKey) {
        RSA rsa = new RSA(org.apache.commons.net.util.Base64.decodeBase64(decryptKey), (byte[])null);
        byte[] decrypt = rsa.decrypt(org.apache.commons.net.util.Base64.decodeBase64(encryptStr), KeyType.PrivateKey);
        String dataStr = new String(decrypt, StandardCharsets.UTF_8);
        return isBase64Encode(dataStr) ? Base64.decodeStr(dataStr) : dataStr;
    }

    public String decrypt(HttpServletRequest request, String encryptedData, Boolean contentTypeByJson, String requestPostStr) {
        return this.decrypt(encryptedData, this.getRSASecretKey().getPrivateKey());
    }

    private static boolean isBase64Encode(String value) {
        if (value != null && value.length() != 0) {
            if (value.length() % 4 != 0) {
                return false;
            } else {
                char[] chars = value.toCharArray();
                char[] var2 = chars;
                int var3 = chars.length;

                for(int var4 = 0; var4 < var3; ++var4) {
                    char chr = var2[var4];
                    if ((chr < 'a' || chr > 'z') && (chr < 'A' || chr > 'Z') && (chr < '0' || chr > '9') && chr != '+' && chr != '/' && chr != '=') {
                        return false;
                    }
                }

                return true;
            }
        } else {
            return false;
        }
    }
}

